package simpleftp.client.control;

import java.io.IOException;

import javax.swing.tree.DefaultMutableTreeNode;

import simpleftp.client.gui.FTPClientWindow;

/** 
 * Abstract class with necessary content to upload and download from the server
 */
public abstract class Loader {
	protected SimpleFTP 		client;
	protected String 			server;
	protected String 			port;
	protected FTPClientWindow 	wind;
	protected String 			user;
	protected String 			pass;
	
	public Loader(String currentServerDir, FTPClientWindow window, String server, String port,
			String user, String pass) {
		this.server = server;
		this.port 	= port;
		this.wind 	= window;
		this.user 	= user;
		this.pass 	= pass;
		client 		= new SimpleFTP(currentServerDir);
	}
	
	/**
	 * List directory content from the server and update client GUI
	 */
	protected void listDir() {
		try {	
			String dirContent = client.ls(wind.useJson.isSelected());
			wind.console.append("\n" + dirContent);
			wind.root.removeAllChildren();
			DefaultMutableTreeNode userRoot = (DefaultMutableTreeNode) client.getDirectoryTree();
			wind.root.add(new DefaultMutableTreeNode(".."));
			wind.root.add(userRoot);
			wind.tree.updateUI();
		} catch (IOException e) {
			wind.console.append("\n" + e.getMessage());
			e.printStackTrace();
		}
	}
}
